package com.zyz.rabbitextensionsdemo.controller;

import com.zyz.rabbitextensionsdemo.constants.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        // 创建一个Message对象设置为持久化或非持久化
        Message message = new Message("consumer ack mode test...".getBytes(),new MessageProperties());
        //message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);// 持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);// 非持久化
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,"ack",message);
        return "消息发送成功~";
    }

    @RequestMapping("/confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm","hello confirm",correlationData);
        return "消息发送成功~";
    }

    @RequestMapping("/returns")
    public String returns() {
        CorrelationData correlationData = new CorrelationData("5");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm111","hello returns",correlationData);
        return "消息发送成功~";
    }

    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"retry","hello retry");
        return "消息发送成功~";
    }

    @RequestMapping("/ttl")
    public String ttl() {
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000"); // 10s
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","hello ttl",messagePostProcessor);
        return "消息发送成功~";
    }

    @RequestMapping("/ttl2")
    public String ttl2() {
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","hello ttl2");
        return "消息发送成功~";
    }

    @RequestMapping("/dl")
    public String dl() {
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","hello dl...");

        // 测试队列长度带来的死信
        String msg = "hello dl...";
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal",msg + i);
        }
        return "消息发送成功~";
    }

    @RequestMapping("/delay")
    public String delay() {
        // 发送消息1（30s）
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelayLong(30_000L);
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","hello delay",messagePostProcessor);
        // 发送消息2（10s）
        MessagePostProcessor messagePostProcessor1 = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelayLong(10_000L);
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","hello delay",messagePostProcessor1);
        System.out.printf("%tc 消息发送成功 \n",new Date());
        return "消息发送成功~";
    }

    @Transactional
    @RequestMapping("/transaction")
    public String transaction() {
        System.out.println("transaction test...");
        transRabbitTemplate.convertAndSend(Constants.TRANSACTION_EXCHANGE,"transaction","transaction1...");
        int num = 3/0;
        transRabbitTemplate.convertAndSend(Constants.TRANSACTION_EXCHANGE,"transaction","transaction2...");
        return "消息发送成功~";
    }

    @RequestMapping("/qos")
    public String qos() {
        System.out.println("qos test...");
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","qos test...");
        }
        return "消息发送成功~";
    }
}
